Imports System.Text
Imports System.Web

Public Class SUM_Blocks
    Private Const Q As String = Chr(34)
    Private mlDatabaseID As Long
    Private msBaseURL As String = ""

    Private mbDataExists As Boolean = False

    Public Sub New( _
                ByVal sBaseURL As String, _
                ByVal lDatabaseID As Long)

        msBaseURL = sBaseURL
        mlDatabaseID = lDatabaseID
    End Sub

    Public ReadOnly Property DataExists() As Boolean
        Get
            Return mbDataExists
        End Get
    End Property

    Public Function Summerize() As String
        Dim oQueueFactory As New SQLAutoDocLib.ScanQueue_Factory

        Dim oDatabase As New SQLAutoDocLib.Database(mlDatabaseID)
        oDatabase.Load()

        '---Get data
        Dim oFactory As New SQLAutoDocLib.BlockLog_Factory
        Dim oData As DataTable = oFactory.AverageDayBlockingCounts(Now.ToShortDateString, mlDatabaseID)
        Dim oSum As New StringBuilder

        With oSum
            .Append("<span style=" & Q & "font-size: 12px; font-weight: bold;" & Q & ">")
            .Append("Database Blocking, " & Now.ToShortDateString)
            .Append("<br/>")
            .Append("</span>")

            .Append("<span style=" & Q & "font-size: 10px; font-weight: normal;" & Q & ">")
            .Append("Last scan: " & oQueueFactory.GetLastQueuedRequest(mlDatabaseID, SQLAutoDocLib.ScanSchedule.Type.BlockScan))
            .Append("<br/>")
            .Append("</span>")

            Dim iMaxCount As Integer = 0
            Dim sURL As String = MakeChartString_BlockCount(oData, "AvgCountOfAllBlocks", iMaxCount)

            .Append("<a href=" & Q & "http://" & msBaseURL & "/SAD/SQBlockingE.aspx?D=" & oDatabase.ID & "&DT=" & HttpUtility.UrlEncode(Now.ToShortDateString) & Q & ">")
            If iMaxCount > 0 Then
                mbDataExists = True

                .Append("<img src=" & Q & sURL & Q & _
                        " alt=" & Q & "Database Blocking for " & oDatabase.Name & " on " & Now.ToShortDateString & Q & _
                        " title=" & Q & "Database Blocking for " & oDatabase.Name & " on " & Now.ToShortDateString & Q & "/>")
            Else
                .Append("No blocking found in period.")
            End If
            .Append("</a>")
        End With

        Return oSum.ToString
    End Function

    Private Function MakeChartString_BlockCount( _
                ByVal oData As DataTable, _
                ByVal sFieldName As String, _
                ByRef iMaxValue As Integer) As String

        Dim oURL As New StringBuilder

        oURL.Append("http://" & msBaseURL & "/SAD/ilc.aspx?T=B&W=100&H=40&TH=2&FG=5959FF&BG=FFFFFF")

        For Each oRow As DataRow In oData.Rows
            If oRow.Item(sFieldName) Is System.DBNull.Value Then
                oURL.Append("&DP=0")
            Else
                oURL.Append("&DP=" & oRow.Item(sFieldName))

                If oRow.Item(sFieldName) > iMaxValue Then
                    iMaxValue = oRow.Item(sFieldName)
                End If
            End If
        Next

        If iMaxValue > 0 Then
            Return oURL.ToString
        Else
            Return ""
        End If
    End Function
End Class
